import { _decorator, Component, Node, Prefab, Event, director, macro, AudioClip, randomRangeInt, find, instantiate, Label, Vec2, v2, EventTouch, PhysicsSystem2D, Layers, v3 } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../Script/Data/SSS_Consts";
import { bdlMgr } from "../../../Script/Manager/BundleManager_S";
// import Global from "./My/AD/platform/Global";
import { SaveData } from "./My/GlobalDefine";
import SaveUtils from "./My/SaveUtils";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

@ccclass('SmallGamePao')
export default class SmallGamePao extends Component {
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;
    @property(Prefab)
    paopao: Prefab | null = null;
    @property(Prefab)
    main: Prefab | null = null;
    @property(Prefab)
    waterEffectPrefab: Prefab | null = null;
    @property(Node)
    Fathermain: Node | null = null;
    @property(Node)
    Fatherpaopao: Node | null = null;
    @property(Node)
    dibu1: Node | null = null;
    @property(Node)
    dibu2: Node | null = null;
    @property(Node)
    MaskUp: Node | null = null;
    @property(Node)
    MaskDown: Node | null = null;
    speed: number = 0
    level: number = 0
    levelspaopao: number[] = [15, 16, 17, 18, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20]
    levelsmain: number[] = [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3]
    nowNum: number = 0
    onLoad() {
        // director.getPhysicsManager().enabled = true;
        // director.getPhysicsManager().gravity = v2(0, -1280);
        PhysicsSystem2D.instance.enable = true;
        PhysicsSystem2D.instance.gravity = v2(0, -1280);


        this.schedule(this.MaskStar, 0.01, macro.REPEAT_FOREVER, 0);

        this.AddListener();
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    On_Button_back() {
        director.loadScene(SSS_Consts.SceneName.MainScene);

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        this.Replay();

        SSS_AudioManager.PlayButtonAudio();
    }
    Replay() {
        this.regame();
    }
    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);

        SaveUtils.inst.getLocalData()
        find("Canvas/Level").getComponent(Label).string = SaveData.PaoPaoLevel + ""
        if (SaveData.PaoPaoLevel > 5) this.speed = 1
        if (SaveData.PaoPaoLevel > 10) this.speed = 1 + SaveData.PaoPaoLevel / 10

        if (SaveData.PaoPaoLevel > this.levelsmain.length - 1) { this.level = this.levelsmain.length - 1 } else { this.level = SaveData.PaoPaoLevel }

        //新手教程
        if (this.level == 1) this.scheduleOnce(() => { find("Canvas/shouzhi").active = true }, 1.3)
        //随机生成关卡
        //水果
        find("Canvas/dibu2/Tips").getComponent(Label).string = "0" + "/" + this.levelsmain[this.level]
        for (let index = 0; index < this.levelsmain[this.level]; index++) {
            let ball = instantiate(this.main)
            ball.setParent(this.Fathermain)
            let i = Math.random() > 0.5 ? 1 : -1
            ball.setPosition(Math.random() * 100 * i, Math.random() * 100 + 1000)
        }
        //气泡
        for (let index = 0; index < this.levelspaopao[this.level]; index++) {
            let ball = instantiate(this.paopao)
            ball.setParent(this.Fatherpaopao)
            let i = Math.random() > 0.5 ? 1 : -1
            ball.setPosition(Math.random() * 250 * i, Math.random() * 100 + 800)

            let scale = Math.random() * 0.8 + 1
            ball.setScale(scale, scale, scale);
        }
        //果篮
        let i = Math.random() > 0.5 ? 1 : -1
        this.dibu1.setPosition(Math.random() * 100 * i, this.dibu1.position.y)
        this.dibu2.setPosition(this.dibu1.position.x, this.dibu1.position.y)
    }
    update(dt) {
        let jiu: Vec2 = v2(this.dibu1.position.x, this.dibu1.position.y)

        this.dibu1.setPosition(jiu.x + this.speed, this.dibu1.position.y)
        this.dibu2.setPosition(this.dibu1.position.x, this.dibu1.position.y)
        // if(this.dibu1.x>250)this.speed=-this.speed
        // if(this.dibu1.x<-250)this.speed=-this.speed
        if (this.dibu1.position.x > 360) this.speed = -this.speed
        if (this.dibu1.position.x < -360) this.speed = -this.speed
    }
    //    //
    next() {
        SaveData.PaoPaoLevel++
        //保存数据
        SaveUtils.inst.SaveData()
        director.loadScene("PaoPaoXiGua_Game_Scene");
    }
    regame() {
        director.loadScene("PaoPaoXiGua_Game_Scene");
    }
    //    /** 松开弹射 */
    private onTouchEnd(e: EventTouch) {
        console.log("onTouchEnd");

        // let temp = director.getPhysicsManager().testPoint(e.touch.getLocation());
        // let selectNode = director.getPhysicsManager().testPoint(e.touch.getLocation());

        // let touchPos = e.getLocation();
        let touchPos = e.getUILocation();
        // 在 3.8 里直接用 PhysicsSystem2D.instance.testPoint
        let selectNode = PhysicsSystem2D.instance.testPoint(touchPos)[0];
        console.log("selectNode", selectNode);
        // console.log("selectNode.node.layer", selectNode.node.layer);
        // console.log("Layers.nameToLayer('paopao')", Layers.nameToLayer('paopao'));


        // if (selectNode && selectNode.node.group == "paopao") {
        // if (selectNode && selectNode.node.layer == Layers.nameToLayer('paopao')) {
        if (selectNode && selectNode.node.layer == 128) {
            console.log("泡泡");

            find("Canvas/shouzhi").active = false
            const eff = instantiate(this.waterEffectPrefab);
            // eff.x = selectNode.node.x;
            // eff.y = selectNode.node.y;
            eff.position = v3(selectNode.node.position.x, selectNode.node.position.y);

            this.Fatherpaopao.addChild(eff);
            // game.emit('game-ball-levelup'); 
            // eff.color = GameData.ballColors[selectNode.getComponent(BallSc).BallIndex - 1];
            selectNode.node.active = false


            let r = randomRangeInt(1, 3);
            let root = `audio/水音效${r}`;
            bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, root, AudioClip).then(res => {
                SSS_AudioManager.Instance.playSFX(res);
            });

            // 胜利的判断
        }
        else {
            // 提示消息
        }
    }
    //    //打开关闭:道具
    OpenClose_Prop(event: Event, customEventData: string) {
        switch (customEventData) {
            case "主页":
                director.loadScene("MainScene");
                // Global.platform.HIDE_BANNER();
                break;
            case "泡泡":
                this.schedule(this.MaskStar, 0.01, macro.REPEAT_FOREVER, 0);
                break
            default:
                break;
        }
    }
    MaskStar(dt: number) {

        // if (this.MaskUp.position.y < 2000) {
        //     this.MaskUp.setPosition(0, this.MaskUp.getPosition().y + dt * 1000)
        //     this.MaskDown.setPosition(0, this.MaskDown.getPosition().y - dt * 1000)
        // }
    }
}